package Plugin
{
	import Objects.GameObject;

	public class TogglePlugin extends GamePlugin
	{
		public var objects:Array=[];
		public var changeCallBack:Function;
		private var m_selectedIndex:int=-1;
		public function TogglePlugin()
		{
			super();
		}
		override protected function _update():void
		{
			var toRemove:Boolean=true;
			
			var index:int=0;
			while(index<objects.length)
			{
				var go:GameObject=objects[index];
				if(!go.removed)
				{
					toRemove=false;
				}
				if(go.mHit)
				{
					select(index);
				}
				index++;
			}
			
			if(toRemove)
			{
				remove();
			}
		}
		public function select(_index:int):void
		{
			if(m_selectedIndex==_index)return;
			
			var index:int=0;
			while(index<objects.length)
			{
				var go:GameObject=objects[index];
				if(index==_index)
				{
					go.selected=true;
				}
				else
				{
					go.selected=false;
				}
				index++;
			}
			
			m_selectedIndex=_index;
			
			if(changeCallBack)
			{
				changeCallBack(m_selectedIndex);
			}
		}
		
		public function get selectedObject():GameObject
		{
			return objects[m_selectedIndex];
		}
		
		public function get selectedIndex():int
		{
			return m_selectedIndex;
		}
	}
}